// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import Tool from "./Tool";
import AudioManage, {soundList} from "./AudioManage";
import gameData from "../scripts/gameData";
import GameConfig from "./GameConfig";

const {ccclass, property} = cc._decorator;

let maxLevel = 50;

let princeLevel = 10;

@ccclass
export default class sceneMediator{

    static showLevel = 2;

    static highLevel = 0;

    static princeShowLevel = 0;

    static princeHighLevel = 0;

    static newUser = false;

    static curEnterLevel = 0;  //0为基础关卡 1为魔王关卡

    /*
        特殊关卡的信息
    */
    //获取
    static getLocalSpecialLevelInformation(){
        let level = Tool.getData("princeLevel");
        if(level == "" || level == null || level == undefined){
            this.princeHighLevel = 8;
            this.princeShowLevel = 0;
        }else{
            this.princeHighLevel = JSON.parse(level)[0];
            this.princeShowLevel = JSON.parse(level)[1];
        }
    }

    //保存
    static savePrinceLevelData(){
        let data = [this.princeHighLevel,this.princeShowLevel];
        Tool.saveData("princeLevel",JSON.stringify(data));
    }

    /*
        普通关卡的信息
    */
    //获取当前使用的关卡
    static getShowScene(){
        return this.showLevel;
    }

    //获取玩家关卡数据
    static getLocalNormalLevelInformation(){
        let level = Tool.getData("TrickyCastle");
        if(level == "" || level == null || level == undefined){
            this.showLevel = 0;
            if(this.highLevel < this.showLevel){
                this.highLevel = this.showLevel;

            }
        }else{
            if(parseInt(level) > maxLevel){
                this.showLevel = 1;
            }else{
                this.showLevel = parseInt(level);
            }

            if(this.highLevel < this.showLevel){
                this.highLevel = this.showLevel;
            }
        }

        this.highLevel = 50;

        if(this.showLevel == 0){
            cc.director.preloadScene("game1");
        }else{
            cc.director.preloadScene("game" + this.showLevel);
        }
    }

    //获取最高的关卡
    static getHighScene(){
        return this.highLevel;
    }

    //获取最大的关卡
    static getLocalHighestLevelInformation(){
        let level = Tool.getData("highLevel");
        if(level == "" || level == null || level == undefined){
            this.highLevel = 0;
        }else{
            this.highLevel = parseInt(level);
        }
    }

    //保存最大的关卡
    static saveHighLevel(){
        Tool.saveData("highLevel",this.highLevel);
    }

    //结算页面完成关卡点击下一关数据
    static finishCurLevel(){
        if(this.curEnterLevel == 0){
            this.showLevel++;
            if(this.showLevel > maxLevel){
                this.showLevel = 1;
            }
        }else{
            this.princeShowLevel++;
            if(this.princeShowLevel > princeLevel){
                this.princeShowLevel = 1;
            }
        }
    }

    //获取最大的关卡数
    static getMaxLevel(){
        return maxLevel;
    }

    /*
        加载下一关
    */
    static LoadNextScene(){
        if(this.curEnterLevel == 0){
            this.showLevel++;
            gameData.clearCurTitle();
            if(this.showLevel > maxLevel){
                this.showLevel = 1;
            }

            cc.director.loadScene("game" + this.showLevel);
        }else{
            this.princeShowLevel++;
            if(this.princeShowLevel > princeLevel){
                this.princeShowLevel = 1;
            }
            cc.director.loadScene("prince" + this.princeShowLevel);
        }
    }

    /*
        加载到当前关卡
    */
    static LoadCurScene(){
        if(this.curEnterLevel == 0){
            gameData.clearCurTitle();
            if(this.showLevel <= 1) {
                this.showLevel = 1;
            }

            cc.director.loadScene("game" + this.showLevel);
        }else{
            if(this.princeShowLevel <= 1) {
                this.princeShowLevel = 1;
            }
            cc.director.loadScene("prince" + this.princeShowLevel);
        }
    }

    /*
        加载到指定关卡
    */
    static LoadToTargetScene(value){
        if(this.curEnterLevel == 0){
            gameData.clearCurTitle();
            if(value > maxLevel){
                value = 1;
            }
            this.showLevel = value;

            cc.director.loadScene("game" + value);
        }else{
            if(value > princeLevel){
                value = 1;
            }
            this.princeShowLevel = value;
            cc.director.loadScene("prince" + value);
        }
    }

    /*
        跳过本关到下一关
    */
    static jumpNextScene(){
        GameConfig.GetInstance().isLockHide = false;
        this.saveLevelData();
        this.LoadNextScene();
    }

    /*
        保存关卡的数据
    */
    static saveLevelData(){
        if(this.curEnterLevel == 0){
            //增加经验
            // let exp  = gameData.judgeAddExp(0,4);
            // setTimeout(()=>{
            //     gameData.addExpValue(exp);
            // },1500);
            if(this.showLevel > this.highLevel){
                this.highLevel = this.showLevel;
                Tool.saveData("TrickyCastle",this.showLevel);
                this.saveHighLevel();
            }
        }else{
            // //增加经验
            // let exp  = gameData.judgeAddExp(0,3);
            // setTimeout(()=>{
            //     gameData.addExpValue(exp);
            // },1500);
            if(this.princeShowLevel > this.princeHighLevel){
                this.princeHighLevel = this.princeShowLevel;
                this.savePrinceLevelData();
            }
        }
    }
    /*
        返回主界面
    */
    static backToHome(){
        GameConfig.GetInstance().isLockHide = false;
        AudioManage.playLevelMusic(soundList.levelBG,true);
        if(this.curEnterLevel == 0){
            gameData.isShowCheats = 0;
            cc.director.loadScene("main",()=>{
                this.curEnterLevel = 0;
            });
        }else{
            cc.director.loadScene("main",()=>{
                this.curEnterLevel = 0;
            });
        }
    }


    static getNewUser(){
        let user = Tool.getData("newUser");
        if(user == "" || user == null){
            this.newUser = true;
            Tool.saveData("newUser",1);
        }else{
            this.newUser = false;
        }
        return this.newUser;
    }

    static returnUser(){
        return this.newUser;
    }
}
